import customerServiceData from "./customerServiceData";

export const initPageInfo = () => {
  customerServiceData.tParams.paramSearch.name = "";
  customerServiceData.tParams.paramSearch.state = null;

  customerServiceData.tPage.listPage.pageNum = 1;
  customerServiceData.tPage.listPage.pageSize = 40;
  customerServiceData.tPage.listPage.total = 0;

  customerServiceData.tData.list.splice(0);
}

/**查询Live Chat 座席列表*/
export const queryCustomerServiceList = (self) => {
  let params = customerServiceData.tParams.paramSearch;
  params.pageNum = customerServiceData.tPage.listPage.pageNum;
  params.pageSize = customerServiceData.tPage.listPage.pageSize;
  self.$Spin.show();
  self.$apiChat.queryCustomerServiceList(params).then(response => {
    self.$Spin.hide();
    customerServiceData.tData.list.splice(0);
    if(response.success) {
      let page = response.result;
      customerServiceData.tData.list.push(...page.list);
      customerServiceData.tPage.listPage.total = page.total;
    }else{
      customerServiceData.tPage.listPage.total = 0;
      console.error(response);
      self.$Message.error(response.errorMessage);
    }
  }).catch(() => {
    self.$Spin.hide();
    customerServiceData.tData.list.splice(0);
    self.$Message.error("查询Live Chat座席列表失败！")
  });
}

/**保存Live Chat 座席*/
export const saveCustomerService = (self) => {
  let params = customerServiceData.tParams.paramAdd;
  self.$Spin.show();
  self.$apiChat.saveCustomerService(params).then(response => {
    self.$Spin.hide();
    if(response.success) {
      customerServiceData.tStatus.modalAdd.isShow = false;
      queryCustomerServiceList(self);
    }else{
      console.error(response);
      self.$Message.error(response.errorMessage);
    }
  }).catch(() => {
    self.$Spin.hide();
    self.$Message.error("保存失败！");
  });
}

/**修改Live Chat 座席*/
export const updateCustomerService = (self) => {
  let params = customerServiceData.tParams.paramModify;
  self.$Spin.show();
  self.$apiChat.updateCustomerService(params).then(response => {
    self.$Spin.hide();
    if(response.success) {
      customerServiceData.tStatus.modalModify.isShow = false;
      queryCustomerServiceList(self);
    }else{
      console.error(response);
      self.$Message.error(response.errorMessage);
    }
  }).catch(() => {
    self.$Spin.hide();
    self.$Message.error("保存失败！");
  });
}

//删除
export const deleteCustomerService = (self, item) => {
  self.$Spin.show();
  self.$apiChat.deleteCustomerService({employeeId: item.employeeId}).then(response => {
    self.$Spin.hide();
    if(response.success) {
      self.$Message.success("Live Chat 座席已删除！");
      queryCustomerServiceList(self);
    }else{
      console.error(response);
      self.$Message.error(response.errorMessage);
    }
  }).catch(() => {
    self.$Spin.hide();
    self.$Message.error("Live Chat 座席删除失败!");
  })
}

//Live Chat座席上线
export const onlineCustomerService = (self, item) => {
  self.$Spin.show();
  self.$apiChat.onlineCustomerService({employeeId: item.employeeId}).then(response => {
    self.$Spin.hide();
    if(response.success) {
      self.$Message.success("Live Chat 座席已上线！");
      queryCustomerServiceList(self);
    }else{
      console.error(response);
      self.$Message.error(response.errorMessage);
    }
  }).catch(() => {
    self.$Spin.hide();
    self.$Message.error("Live Chat 座席上线失败!");
  })
}

//Live Chat座席上线
export const offlineCustomerService = (self, item) => {
  self.$Spin.show();
  self.$apiChat.offlineCustomerService({employeeId: item.employeeId}).then(response => {
    self.$Spin.hide();
    if(response.success) {
      self.$Message.success("Live Chat 座席已下线！");
      queryCustomerServiceList(self);
    }else{
      console.error(response);
      self.$Message.error(response.errorMessage);
    }
  }).catch(() => {
    self.$Spin.hide();
    self.$Message.error("Live Chat 座席下线失败!");
  })
}
